﻿using ApplicationModels.A02.Identities;
using Commons.Responses;
using EntityModels.E02.Identities;
using EntityModels.E09.Files;
using IApplicationServices;
using Microsoft.AspNetCore.Mvc;

namespace WebAPI.Controllers
{
    [Route("[controller]/[action]")]
    public class UserController:BaseController<Guid,AppUser,AppUserAM>
    {
        private readonly IUserService apiService;
        private readonly IWebHostEnvironment _env;
        private readonly IFilePathService pathService;
        public UserController(IUserService apiService, IWebHostEnvironment env, IFilePathService pathService) : base(apiService)
        {
            this.apiService = apiService;
            _env = env;
            this.pathService = pathService;
        }

        [HttpPost]
        public async Task<MessageResponse> LockoutAsync(AppUserAM apm)
        {

            return await apiService.Lockout(apm.Id);
        }
        
        [HttpGet]
        public async Task<DataResponse<AppUserAM>> GetUserByMyselfAsync()
        {
            return await ApiService.GetByIdAsync(Guid.Parse(AppUserId));
        }
        /// <summary>
        /// 更新头像
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>

        [HttpPost]
        public async Task<MessageResponse> UpdateAvatar(AppUserAM user)
        {
            if(user.Id != Guid.Parse(AppUserId))
            {
                return new MessageResponse("违规操作!", false);
            }
            return await apiService.UpdateAvatar(user);

        }
        /// <summary>
        /// 根据用户Id获取用户头像Id
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<string> GetAvatarByUserIdAsync(string userId)
        {
            if(Guid.TryParse(userId,out Guid id))
            {
                return await apiService.GetAvatarByUserIdAsync(id);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 根据用户Id获取用户头像
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MemoryStream> GetAvatarFileByUserIdAsync(string userId)
        {
            if(Guid.TryParse(userId, out Guid id)){
                var avatar =  await apiService.GetAvatarByUserIdAsync(id);
                
               if(avatar != null)
                {
                    var contentRootPath = _env.ContentRootPath;
                    var file = await pathService.FindAsync(Guid.Parse(avatar));
                    if (file == null)
                    {
                        file = await pathService.FindAsync(Guid.Parse("051822f6-be6e-4b56-91ae-73e030369c97"));
                    }
                    var imgPath = contentRootPath + file.Path;
                    try
                    {
                        var imgStream = new MemoryStream(System.IO.File.ReadAllBytes(imgPath));
                        return imgStream;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                        return null;
                    }
                }
            }
            return null;
        }

    }
}
